﻿using Microsoft.AspNetCore.Authorization;
using Simple.Services;
using Simple.Services.System;
using Simple.Services.System.ClassRoom;
using Simple.Services.System.Recipes.Models;

namespace Simple.WebApi.Controllers.System
{
    [Route("api/InspectionRecord/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class InspectionRecord : ControllerBase
    {
        public readonly InspectionRecordService  _inspectionRecordService;
        public InspectionRecord(InspectionRecordService  inspectionRecordService)
        {
            _inspectionRecordService = inspectionRecordService;
        }

        /// <summary>
        /// 巡检记录列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Page([FromQuery] InspectionRecordQueryModel model)
        {
            var data = await _inspectionRecordService.GetPageAsync(model);
            return AppResult.Status200OK(data: data);
        }


        /// <summary>
        /// 巡检详情
        /// </summary>
        /// <param name="inspectionId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Datail([FromQuery] Guid inspectionId)
        {
            var data = await _inspectionRecordService.GetDatailAsync(inspectionId);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 获取教室信息
        /// </summary>
        /// <param name="gardenHouseClassRoomId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> RoomInfo([FromQuery] Guid gardenHouseClassRoomId)
        {
            var data = await _inspectionRecordService.GetRoomInfo(gardenHouseClassRoomId);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 教室最新一条巡检记录
        /// </summary>
        /// <param name="gardenHouseClassRoomId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> LatestInspection([FromQuery] Guid gardenHouseClassRoomId)
        {
            var data = await _inspectionRecordService.GetLatestInspection(gardenHouseClassRoomId);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 教室巡检历史
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> InspectionHistoryPage([FromQuery] Guid gardenHouseClassRoomId)
        {
            var data = await _inspectionRecordService.GetInspectionHistoryPage(gardenHouseClassRoomId);
            return AppResult.Status200OK(data: data);
        }


        /// <summary>
        /// 新增巡检
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Add([FromBody] InspectionRecordAddDto model)
        {
            await _inspectionRecordService.AddAsync(model);
            return AppResult.Status200OK("新增成功");
        }

        /// <summary>
        /// 修改巡检
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Edit([FromForm] InspectionRecordDto model)
        {
            await _inspectionRecordService.EditAsync(model);
            return AppResult.Status200OK("修改成功");
        }

        /// <summary>
        /// 修改巡检
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> EditV2([FromBody] InspectionRecordDto model)
        {
            await _inspectionRecordService.EditAsync(model);
            return AppResult.Status200OK("修改成功");
        }


        /// <summary>
        /// 巡检删除
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
        {
            await _inspectionRecordService.DeleteAsync(models.Select(m => m.Id));
            return AppResult.Status200OK("删除成功");
        }

    }
}
